package P3;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.util.Arrays;

public class AddressConverter
{	
	/*public static byte[] GetBytes(String address)
	{
		byte[] bytes = new byte[8];

		bytes[0] = (byte)(Integer.parseInt(address.substring(0, address.indexOf('.'))) - 128);
		address = address.substring(address.indexOf('.') + 1);
		bytes[1] = (byte)(Integer.parseInt(address.substring(0, address.indexOf('.'))) - 128);
		address = address.substring(address.indexOf('.') + 1);
		bytes[2] = (byte)(Integer.parseInt(address.substring(0, address.indexOf('.'))) - 128);
		address = address.substring(address.indexOf('.') + 1);
		bytes[3] = (byte)(Integer.parseInt(address.substring(0, address.indexOf(':'))) - 128);
		address = address.substring(address.indexOf(':') + 1);
		int port = Integer.parseInt(address);
		bytes[4] = (byte)((port      ) & 0x00FF);
		bytes[5] = (byte)((port >>> 8 ) & 0x00FF);
		bytes[6] = (byte)((port >>> 16) & 0x00FF);
		bytes[7] = (byte)((port >>> 24) & 0x00FF);
		
		return bytes;
	}
	
	public static String GetString(byte[] bytes)
	{
		StringBuilder output = new StringBuilder();
	
		output.append(GetHostString(bytes));
		output.append(':');
		output.append(GetPort(bytes));
		
		return output.toString();
	}
	
	public static String GetHostString(byte[] bytes)
	{
		StringBuilder output = new StringBuilder();
		
		output.append(bytes[0] + 128);
		output.append('.');
		output.append(bytes[1] + 128);
		output.append('.');
		output.append(bytes[2] + 128);
		output.append('.');
		output.append(bytes[3] + 128);
		
		return output.toString();
	}
	
	public static int GetPort(byte[] bytes)
	{
		int port = 0;
		
		port += (bytes[4] & 0xFF);
		port += (bytes[5] & 0xFF) << 8;
		port += (bytes[6] & 0xFF) << 16;
		port += (bytes[7] & 0xFF) << 24;
		
		return port;
	}*/
	
	public static long GetBytes(String address)
	{
		long bytes = 0;
		bytes += (Integer.parseInt(address.substring(0, address.indexOf('.')).trim()) & 0xFF);
		address = address.substring(address.indexOf('.') + 1);
		bytes += ((Integer.parseInt(address.substring(0, address.indexOf('.')).trim()) & 0xFF) << 8);
		address = address.substring(address.indexOf('.') + 1);
		bytes += ((Integer.parseInt(address.substring(0, address.indexOf('.')).trim()) & 0xFF) << 16);
		address = address.substring(address.indexOf('.') + 1);
		bytes += ((Integer.parseInt(address.substring(0, address.indexOf(':')).trim()) & 0xFF) << 24);
		address = address.substring(address.indexOf(':') + 1);
		
		int port = Integer.parseInt(address);
		bytes += (((long)(port & 0xFFFFFFFF)) << 32);
		
		return bytes;
	}
	
	public static String GetString(long bytes)
	{
		StringBuilder output = new StringBuilder();
	
		output.append(GetHostString(bytes));
		output.append(':');
		output.append(GetPort(bytes));
		
		return output.toString();
	}
	
	public static String GetHostString(long bytes)
	{
		StringBuilder output = new StringBuilder();
		
		output.append((bytes) & 0xFF);
		output.append('.');
		output.append((bytes >> 8) & 0xFF);
		output.append('.');
		output.append((bytes >> 16) & 0xFF);
		output.append('.');
		output.append((bytes >> 24) & 0xFF);
		
		return output.toString();
	}
	
	public static int GetPort(long bytes)
	{
		return (int)((bytes >> 32));
	}
}
